<template>
	<view class="sell_list_alert">
		<view class="close" @click="$emit('close')">
			<image :src="`${baseImageUrl}/common/close1.png`"></image>
		</view>
		<view class="title">
			<view>在售{{text}}</view>
		</view>
		<view class="header">
			<view class="item" @click="change_number">
				<text>售卖数量</text>
				<image :src="`${baseImageUrl}/common/px.png`"></image>
			</view>
			<view class="item" @click="change_price">
				<text>售卖单价</text>
				<image :src="`${baseImageUrl}/common/px.png`"></image>
			</view>
		</view>
		<scroll-view scroll-y class="scroll_view" @scrolltolower="loadmore">
			<view class="item" v-for="item in list">
				<view class="number">
					<image :src="`${baseImageUrl}/common/hot_cover1.png`" v-if="item.field=='foodstuff'"></image>
					<image :src="`${baseImageUrl}/common/hot_cover2.png`" v-else-if="item.field=='cloth'"></image>
					<image :src="`${baseImageUrl}/common/hot_cover3.png`" v-else-if="item.field=='titledeed'"></image>
					<image :src="`${baseImageUrl}/common/hot_cover4.png`" v-else-if="item.field=='music'"></image>
					<image :src="`${baseImageUrl}/common/hot_cover5.png`" v-else></image>
					<text>{{item.surplus_number}}</text>
				</view>
				<view>￥{{item.price}}/担</view>
				<view>￥{{item.surplus_total}}</view>
				<view class="btn" @click="buy(item)">购买</view>
			</view>
			<u-loadmore :status="loadmore_status" :color="$store.state.loadmoreColor"
				:lineColor="$store.state.loadmoreColor" line @loadmore="loadmore" />
		</scroll-view>
		<u-popup :show="popup_show" bgColor="transparent" mode="center" @close="close">
			<BuyPopup :field="field" :text="text" :id="id" :total="total" :price="price" @close="close" />
		</u-popup>
	</view>
</template>

<script>
	import {
		userSellLists
	} from '@/request/api.js';
	import BuyPopup from '@/components/home/BuyPopup.vue';
	export default {
		components: {
			BuyPopup
		},
		props: {
			text: {
				type: String,
				default: ""
			},
			field: {
				type: String,
				default: ""
			}
		},
		data() {
			return {
				number_sort: "desc",
				price_sort: "desc",
				list: [],

				id: null,
				total: 0,
				price: 0,
				popup_show: false
			}
		},
		created() {
			this.getList();
		},
		methods: {
			async getList() {
				let {
					list
				} = await userSellLists({
					page: this.page,
					limit: this.limit,
					number_sort: this.number_sort,
					price_sort: this.price_sort,
					field: this.field
				});
				this.last_page = list.last_page;
				list = list.list;
				this.list = this.page == 1 ? list : [...this.list, ...list];
				this.loadmore_status = this.last_page == this.page ? 'nomore' : 'loadmore';
			},
			loadmore() {
				if (this.page < this.last_page) {
					this.page++;
					this.getList();
				}
			},
			change_number() {
				this.page = 1;
				this.list = [];
				this.price_sort = "";
				this.number_sort = this.number_sort == "desc" ? "asc" : "desc";
				this.getList();
			},
			change_price() {
				this.page = 1;
				this.list = [];
				this.number_sort = "";
				this.price_sort = this.price_sort == "desc" ? "asc" : "desc";
				this.getList();
			},
			buy(item) {
				this.id = item.id;
				this.price = item.price;
				this.total = item.surplus_number;
				this.popup_show = true;
			},
			close() {
				this.page = 1;
				this.list = [];
				this.getList();
				this.popup_show = false;
			}
		}
	}
</script>

<style scoped lang="scss">
	.sell_list_alert {
		width: 100vw;
		height: 1110rpx;
		padding-top: 45rpx;
		box-sizing: border-box;
		background: url(#{$baseImageUrl}/bg/750_1110.png) no-repeat;
		background-size: 100% 100%;
		position: relative;

		.close {
			width: 122rpx;
			height: 122rpx;
			position: absolute;
			right: 0rpx;
			top: 0rpx;
			transform: translate(20rpx, -45rpx);
			z-index: 2;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			display: flex;
			justify-content: center;
			align-items: center;

			view {
				width: 256rpx;
				height: 30rpx;
				line-height: 30rpx;
				text-align: center;
				color: #494338;
				font-size: 42rpx;
				font-weight: 400;
				font-family: LiSu;
				background: url(#{$baseImageUrl}/home/title.png) no-repeat;
				background-size: 100% 100%;
			}
		}

		.header {
			// height: 90rpx;
			padding-left: 60rpx;
			padding-top: 42rpx;
			box-sizing: border-box;
			display: flex;

			.item {
				width: 194rpx;
				height: 50rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/home/i4.png) no-repeat;
				background-size: 100% 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;

				text {
					color: #FFFFFF;
					font-size: 28rpx;
					font-weight: 400;
					font-family: SimSun;
				}

				image {
					width: 13rpx;
					height: 18rpx;
				}
			}
		}

		.scroll_view {
			height: calc(100% - 92rpx - 30rpx);
			padding: 20rpx 48rpx 10rpx 40rpx;
			box-sizing: border-box;

			.item {
				height: 120rpx;
				color: #494338;
				font-size: 26rpx;
				font-weight: 400;
				font-family: SimSun;
				border-bottom: 1px solid rgba(163, 159, 160, 0.5);
				padding: 0 18rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.number {
					display: flex;
					align-items: center;

					image {
						width: 35rpx;
						height: 36rpx;
					}
				}

				.btn {
					width: 140rpx;
					height: 70rpx;
					line-height: 70rpx;
					text-align: center;
					color: #FFFFFF;
					font-size: 30rpx;
					background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
					background-size: 100% 100%;
				}
			}
		}
	}
</style>